package com.zhao.service.serviceImpl;

import com.zhao.dao.*;
import com.zhao.pojo.*;
import com.zhao.service.LoginService;
import com.zhao.service.PublicizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {
    @Autowired
    private StudentDao studentDao;

    @Autowired
    private SchoolCounsellorDao schoolCounsellorDao;

    @Autowired
    private UniversityDao universityDao;

    @Autowired
    private SuperviserDao superviserDao;

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private MajorDao majorDao;

    @Autowired
    private AdminDao adminDao;

    @Autowired
    private PublicizeService publicizeService;


    @Override
    public Student findByStudentID(String StudentID, String universityID) {
        return studentDao.findByStudentID(StudentID,universityID);
    }

    @Override
    public String schoolCounsellorLogin(User user, HttpSession session) {

        University university = universityDao.findByName(user.getUniversityName());
        if(university == null){
            System.out.println("辅导员登录 学校名称不正确");
            return "学校名称不正确";
        }

        SchoolCounsellor schoolCounsellor = schoolCounsellorDao.findByID(user.getUsername(),university.getUniversityID());
        System.out.println(schoolCounsellor);
        if(schoolCounsellor == null){
            System.out.println("辅导员登录  用户名不存在");
            return "用户名不存在";
        }

        if(!schoolCounsellor.getPassword().equals(user.getPassword())){
            System.out.println("辅导员登录  密码不正确");
            return "密码不正确";
        }
        user.setUserID(user.getUsername());
        user.setUsername(schoolCounsellor.getName());
        user.setUniversityID(university.getUniversityID());
        session.setAttribute("user",user);
        session.setAttribute("schoolCounsellor",schoolCounsellor);

        SchoolCounsellorImpl schoolCounsellorImpl = new SchoolCounsellorImpl();
        schoolCounsellorImpl.setSchoolCounsellorID(schoolCounsellor.getSchoolCounsellorID());
        schoolCounsellorImpl.setName(schoolCounsellor.getName());
        schoolCounsellorImpl.setUniversityID(schoolCounsellor.getUniversityID());
        schoolCounsellorImpl.setUniversityName(university.getUniversityName());
        schoolCounsellorImpl.setDepartmentID(schoolCounsellor.getDepartmentID());

        Department department = departmentDao.findByID(schoolCounsellor.getDepartmentID(),schoolCounsellor.getUniversityID());
        schoolCounsellorImpl.setDepartmentName(department.getDepartmentName());

        schoolCounsellorImpl.setEmail(schoolCounsellor.getEmail());
        schoolCounsellorImpl.setGender(schoolCounsellor.getGender());
        schoolCounsellorImpl.setImage(schoolCounsellor.getImage());
        schoolCounsellorImpl.setMajorID(schoolCounsellor.getMajorID());
        Major major = majorDao.findByID(schoolCounsellor.getMajorID(),schoolCounsellor.getUniversityID());
        schoolCounsellorImpl.setMajorName(major.getMajorName());

        schoolCounsellorImpl.setIsRegisted(schoolCounsellor.getIsRegisted());
        schoolCounsellorImpl.setPassword(schoolCounsellor.getPassword());
        schoolCounsellorImpl.setPhoneNumber(schoolCounsellor.getPhoneNumber());

        session.setAttribute("schoolCounsellorImpl",schoolCounsellorImpl);


        return "success";
    }

    @Override
    public String superviserLogin(User user, HttpSession session) {
        University university = universityDao.findByName(user.getUniversityName());
        if(university == null){
            System.out.println("校警登录 学校名称不正确");
            return "学校名称不正确";
        }

        Superviser superviser = superviserDao.findByID(user.getUsername(),university.getUniversityID());
        System.out.println(superviser);
        if(superviser == null){
            System.out.println("校警登录  用户名不存在");
            return "用户名不存在";
        }

        if(!superviser.getPassword().equals(user.getPassword())){
            System.out.println("校警登录  密码不正确");
            return "密码不正确";
        }
        user.setUserID(user.getUsername());
        user.setUsername(superviser.getSuperviserID());
        user.setUniversityID(superviser.getUniversityID());
        session.setAttribute("user",user);
        session.setAttribute("superviser",superviser);
        ///查询最新公告
        String res = publicizeService.getNew3Publicize(session,superviser.getUniversityID());
        System.out.println(res);
        if(!res.equals("success")){
            System.out.println("get new 3 error");
        }
        return "success";
    }

    @Override
    public String adminLogin(User user, HttpSession session) {

        University university = universityDao.findByName(user.getUniversityName());

        if(university == null){
            session.setAttribute("loginMessage","学校名称错误");
            return "error";
        }

        Admin admin = adminDao.findByID(user.getUsername(),university.getUniversityID());
        if(admin == null){
            session.setAttribute("loginMessage","用户名错误");
            return "error";
        }
        if(!user.getPassword().equals(admin.getPassWord())){
            session.setAttribute("loginMessage","密码错误");
            return "error";
        }

        session.setAttribute("admin",admin);
        user.setUsername(admin.getName());
        user.setUserID(admin.getAdminID());
        user.setUniversityID(admin.getUniversityID());
        session.setAttribute("user",user);

        ///查询最新公告
        String res = publicizeService.getNew3Publicize(session,admin.getUniversityID());
        System.out.println(res);
        if(!res.equals("success")){
            System.out.println("get new 3 error");
        }


        return "success";
    }
}
